package com.company.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.project.common.aop.annotation.DataScope;
import com.company.project.common.utils.DataResult;
import com.company.project.entity.AdCost;
import com.company.project.entity.CostEntity;
import com.company.project.service.AdCostService;
import com.company.project.service.SysCostService;
import com.company.project.util.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 订单管理
 *
 * @author Jake
 * @version V1.0
 */
@Slf4j
@Api(tags = "费用管理")
@RestController
@RequestMapping("/sysCost")
public class SysCostController {
    @Resource
    private SysCostService sysCostService;
    @Autowired
    private AdCostService adCostService;

    @ApiOperation(value = "新增")
    @PostMapping("/add")
    @RequiresPermissions("sysCost:add")
    public DataResult add(@RequestBody CostEntity sysCost) {
        sysCostService.save(sysCost);
        return DataResult.success();
    }


    @ApiOperation(value = "删除")
    @DeleteMapping("/delete")
    @RequiresPermissions("sysCost:delete")
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids) {
        sysCostService.removeByIds(ids);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("/update")
    @RequiresPermissions("sysCost:update")
    public DataResult update(@RequestBody CostEntity sysCost) {
        sysCostService.updateById(sysCost);
        return DataResult.success();
    }

    @ApiOperation(value = "查询分页数据")
    @PostMapping("/listByPage")
    @RequiresPermissions("sysCost:list")
    @DataScope
    public DataResult findListByPage(@RequestBody CostEntity sysCost) {
        System.out.println("*************[findListByPage] , findListByPage.....sysCost"+sysCost);
        Page page = new Page(sysCost.getPage(), sysCost.getLimit());
        LambdaQueryWrapper<CostEntity> queryWrapper = Wrappers.lambdaQuery();
//        //查询条件示例
        if (!StringUtils.isEmpty(sysCost.getCreateTime())) {
            queryWrapper.gt(CostEntity::getCreateTime, sysCost.getCreateTime());
        }
//
        IPage<CostEntity> iPage = sysCostService.page(page, queryWrapper);
        return DataResult.success(iPage);
    }

    @ApiOperation(value = "查询广告费分页数据")
    @GetMapping("/adCost")
    public DataResult getAdcostList(@RequestParam(required = false,defaultValue = "1") Integer pageIndex,
                                    @RequestParam(required = false,defaultValue = "10") Integer pageSize,
                                    @RequestParam(required = false) String name) {
        Page page = new Page(pageIndex, pageSize);
        QueryWrapper<AdCost> qw = new QueryWrapper<>();
        if (!StringUtils.isEmpty(name)){
            qw.eq("name",name);
        }
        Page<AdCost> iPage = adCostService.page(page, qw);
        return DataResult.success(iPage);
    }
}
